package com.jp.knowledge.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.widget.EditText;

import com.jiayen.util.DisplayUtil;
import com.jp.knowledge.R;

/**
 * @author gzejia 978862664@qq.com
 * @creat 2016/4/5 22:03
 */
public class SearchEditText extends EditText {

    // 上下文
    private Context mContext;
    // Canvas画笔
    private Paint mPaint;
    // 圆角边框
    private RectF mRectF;
    // 搜索按钮Bmp
    private Bitmap mSearchBmp;
    // “搜索”文本距离顶部位置
    private float mBaseline;
    // true_绘制搜索按钮与“搜索”文本
    private boolean mIsDrawImgTv;

    public SearchEditText(Context context) {
        super(context);
        this.mContext = context;
        initView();
    }

    public SearchEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        initView();
    }

    public SearchEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        initView();
    }

    private void initView() {
        mPaint = new Paint();
        mPaint.setColor(mContext.getResources().getColor(R.color.theme_color));
        mPaint.setTextSize(DisplayUtil.sp2px(mContext, 16));
        mPaint.setStrokeWidth(1.5f);
        mSearchBmp = BitmapFactory.decodeResource(mContext.getResources(), R.mipmap.guanzhusousuo);
        this.setBackgroundResource(0);
        this.setPadding(mSearchBmp.getWidth() * 2, 5, 5, 5);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        float edWidth = getWidth();
        float edHeight = getHeight();
        float edSpace = 5.0f;

        if (mRectF == null) {
            mRectF = new RectF();
            mRectF.left = edSpace;
            mRectF.top = edSpace;
            mRectF.right = edWidth - edSpace;
            mRectF.bottom = edHeight - edSpace;

            // 实现垂直居中_http://blog.csdn.net/hursing/article/details/18703599
            Paint.FontMetricsInt fontMetrics = mPaint.getFontMetricsInt();
            mBaseline = (mRectF.bottom + mRectF.top - fontMetrics.bottom - fontMetrics.top) / 2;
        }

        mPaint.setStyle(Paint.Style.STROKE);
        canvas.drawRoundRect(mRectF, edHeight / 2, edHeight / 2, mPaint);
        mPaint.setStyle(Paint.Style.FILL);

        float srWidth = mSearchBmp.getWidth();
        float srHeight = mSearchBmp.getHeight();
        if (mIsDrawImgTv) {
            canvas.drawBitmap(mSearchBmp, srWidth / 2, (edHeight - srHeight) / 2, mPaint);
        } else {
            canvas.drawBitmap(mSearchBmp, edWidth / 2 - srWidth * 4 / 3, (edHeight - srHeight) / 2, mPaint);
            canvas.drawText("搜索", edWidth / 2 , mBaseline, mPaint);
        }
    }

    @Override
    protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
        super.onFocusChanged(focused, direction, previouslyFocusedRect);
        mIsDrawImgTv = focused || !TextUtils.isEmpty(getText());
        invalidate();
    }
}
